package zhr.store.controller;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.format.annotation.DateTimeFormat;
import zhr.store.Dto.OrdersDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhr.store.common.BaseContext;
import zhr.store.common.Result;
import zhr.store.domain.OrderDetail;
import zhr.store.domain.Orders;
import zhr.store.service.OrderDetailService;
import zhr.store.service.OrdersService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    /**
     * 用户下单
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return Result.success("下单成功");
    }

    /**
     * 分页查询用户订单
     */
    @GetMapping("/userPage")
    public Result<Page> page(int page, int pageSize) {
        //构建page分页插件
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page, pageSize);
        //构建查询条件
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersLambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(ordersPage, ordersLambdaQueryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        List<Orders> orders = ordersPage.getRecords();
        List<OrdersDto> ordersDtos = new ArrayList<>();
        for (Orders order : orders) {
            //拷贝对象
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(order, ordersDto);
            //获取订单id
            Long id = order.getId();
            //构建查询条件
            LambdaQueryWrapper<OrderDetail> detailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            detailLambdaQueryWrapper.eq(OrderDetail::getOrderId, id);
            //查询
            List<OrderDetail> list = orderDetailService.list(detailLambdaQueryWrapper);
            ordersDto.setOrderDetails(list);
            ordersDtos.add(ordersDto);
        }
        ordersDtoPage.setRecords(ordersDtos);
        return Result.success(ordersDtoPage);
    }
    /**
     * 根据id修改订单状态
     */
    @PutMapping
    public Result<String> update(@RequestBody Orders orders){
        //构建修改查询条件
        LambdaUpdateWrapper<Orders> ordersLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        ordersLambdaUpdateWrapper.eq(Orders::getId,orders.getId());
        ordersLambdaUpdateWrapper.set(Orders::getStatus,3);
        //修改状态
        ordersService.update(ordersLambdaUpdateWrapper);
        return Result.success("派送成功");
    }
    /**
     * 分页查询订单
     */
    @GetMapping("/page")
    public Result<Page> selectAll(int page, int pageSize, Long id,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime) {
        //构建分页插件
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构建查询条件
        if (id != null) {
            lambdaQueryWrapper.eq(Orders::getId, id);
        }
        if (beginTime != null && endTime != null) {
            lambdaQueryWrapper.ge(Orders::getOrderTime, beginTime)
                    .le(Orders::getOrderTime, endTime);
        }
        lambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(ordersPage, lambdaQueryWrapper);
        return Result.success(ordersPage);
    }
}
